import * as data from './data';
import log from '../../../log';
import * as util from '../../../util';

export function fetch(query, page, pageSize) {
    return util.resolve().then(() => {
        log.info('fetch dict params----', query);
        const pageOpt = {
            page,
            pageSize
        };
        query = query ? [query] : null;
        return data.fetch(query, 0, pageOpt).then(res => {
            log.info('fetch dict result----', res.toJSON());
            return util.data(res, 1);
        });
    }).catch(err => {
        log.error('fetch dict is err----', err);
        return err;
    });
}
export function find(query) {
    return util.resolve().then(() => {
        log.info('find dict params----', query);
        query = query ? [query] : null;
        return data.find(query, 0).then(res => {
            log.info('find dict result----', util.data(res));
            return {data: util.data(res)};
        });
    }).catch(err => {
        log.error('find dict is err----', err);
        return err;
    });
}
export function getCategory() {
    return find({status: 1, isSys: 1}).then(res => {
        log.info('find dict result----', res);
        return res.data;
    }).catch(err => {
        log.error('getCategory dict is err----', err);
        return err;
    });
}
export function add(dict, ctx) {
    return util.resolve().then(() => {
        log.info('add dict params----', dict);
        return data.find([['dictType', '=', `${dict.dictType}`], ['name', '=', `${dict.name}`]]).then(res => {
            if (res.toJSON().length > 0) {
                throw '字典名称重复！';
            }
            return data.find([['dictType', '=', `${dict.dictType}`], ['code', '=', `${dict.code}`]]).then(res => {
                if (res.toJSON().length > 0) {
                    throw '字典编码重复！';
                }
                return data.add(dict).then(res => {
                    log.info('add dict result----', util.data(res));
                    return util.data(res);
                });
            });
        });
    }).catch(err => {
        log.error('add dict is err----', err);
        return ctx.throw(500, err);
    });
}
export function edit(dict, ctx) {
    return util.resolve().then(() => {
        log.info('edit dict params----', dict);
        return data.find([['dictType', '=', `${dict.dictType}`], ['id', '<>', `${dict.id}`], ['name', '=', `${dict.name}`]], 1).then(res => {
            if (res.toJSON().length > 0) {
                throw '字典名称重复！';
            }
            return data.find([['dictType', '=', `${dict.dictType}`], ['id', '<>', `${dict.id}`], ['code', '=', `${dict.code}`]], 1).then(res => {
                if (res.toJSON().length > 0) {
                    throw '字典编码重复！';
                }
                return data.edit(dict).then(res => {
                    log.info('edit dict result----', util.data(res));
                    return util.data(res);
                });
            });
        });
    }).catch(err => {
        log.error('edit dict is err----', err);
        return ctx.throw(500, err);
    });
}
export function del(id) {
    return util.resolve().then(() => {
        log.info('delete dict params----', id);
        return data.del(id).then(res => {
            log.info('delete dict result----success', util.data(res));
            return util.data(res);
        });
    }).catch(err => {
        log.error('delete dict is err----', err);
        return err;
    });
}
